package com.mall.manager.service;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.mall.common.attributes.EnumType;
import com.mall.common.dao.MysqlDBManager;
import com.mall.untils.DateUtils;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

@Service
public class BabyTopService {

	public static Logger logger = Logger.getLogger(BabyTopService.class);

	/**
	 * 批量插入宝宝单题信息到rds
	 * @param para
	 * @param sql
	 */
	public static void insertBatch(Map<String,Map<String, Object>> para){
		Connection conn =null;
		PreparedStatement ps=null;
		try {
			String sql = "insert into t_single_topic(topic_id,bady_id,garden_id,teacher_id,topic_time,data_type,create_time,update_time,topic_name,baby_name,garden_name,teacher_name) values(?,?,?,?,?,?,?,?,?,?,?,?)";
			 conn = MysqlDBManager.getConnection();
			 ps = (PreparedStatement) conn.prepareStatement(sql);
			if(para!=null&&para.size()>0){
				for(String key:para.keySet()){
					ps.setInt(1, MapUtils.getInteger(para.get(key), "topic_id"));
					ps.setInt(2, MapUtils.getInteger(para.get(key), "baby_id"));
					ps.setInt(3, MapUtils.getInteger(para.get(key), "garden_id"));
					ps.setInt(4, MapUtils.getInteger(para.get(key), "teacher_id"));
					ps.setDouble(5, MapUtils.getDouble(para.get(key), "topic_time"));
					ps.setInt(6, EnumType.DATA_TYPE_STATUS.getType(MapUtils.getString(para.get(key), "data_type")));
					ps.setTimestamp(7, Timestamp.valueOf(MapUtils.getString(para.get(key), "commit_time")));
					ps.setTimestamp(8, DateUtils.getTimestamp());
					ps.setString(9, MapUtils.getString(para.get(key), "topic_name"));
					ps.setString(10, MapUtils.getString(para.get(key), "baby_name"));
					ps.setString(11, MapUtils.getString(para.get(key), "garden_name"));
					ps.setString(12, MapUtils.getString(para.get(key), "teacher_name"));
					ps.addBatch();
				}
				ps.executeBatch();
				conn.commit();
			}
		} catch (SQLException e) {
			logger.info("插入查询异常："+e.getMessage());
		}finally{
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


